package com.miniits.base.utils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: WWW.MINIITS.COM
 * @Date: 2018/1/23
 * @Time: 21:35
 * <p>
 * Description:
 * ***
 */
public class HTMLSpirit {

    public static final Map<String, List<String>> keys = new HashMap<String, List<String>>() {{
        put("沁灵", Arrays.asList("【", "】"));
        put("杀尾", Arrays.asList("【", "】"));
        put("襡醉计划", Arrays.asList("五星定位胆", " \\["));
        put("红马", Arrays.asList("【", "】"));
        put("宝贝", Arrays.asList("宝贝五星", "见好就收"));
        put("财神", Arrays.asList("五星定位胆\\(", "\\)"));
        put("巫妖五", Arrays.asList("五星定位胆★", " \\["));
        put("黑马", Arrays.asList("【", "】"));
        put("平安", Arrays.asList("【", "】"));
        put("紫红", Arrays.asList("【", "】"));
        put("紫光", Arrays.asList("【", "】"));
        put("彩霸计划", Arrays.asList("五星定位胆\\(", "\\)"));
    }};

    public static String getKey(String html) {
        String key = null;
        for (String s : keys.keySet()) {
            if (html.indexOf(s) >= 0) {
                key = s;
                break;
            }
        }
        return key;
    }

    public static String getVal(List<String> els, String key) {
        String val = null;
        String txt = els.get(els.size() - 1);
        for (String s : keys.keySet()) {
            if (key.equals(s)) {
                val = txt.split(keys.get(s).get(0))[1].split(keys.get(s).get(1))[0];
                break;
            }
        }
        return val;
    }

    public static String delHTMLTag(String htmlStr) {
        String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
        String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
        String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式

        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); //过滤script标签

        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); //过滤style标签

        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); //过滤html标签

        return htmlStr.trim(); //返回文本字符串
    }

    /**
     * 获取指定HTML标签的指定属性的值
     *
     * @param source  要匹配的源文本
     * @param element 标签名称
     * @param attr    标签的属性名称
     * @return 属性值列表
     */
    public static List<String> match(String source, String element, String attr) {
        List<String> result = new ArrayList<String>();
        String reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>";
        Matcher m = Pattern.compile(reg).matcher(source);
        while (m.find()) {
            String r = m.group(1);
            result.add(r);
        }
        return result;
    }

}
